package com.jf.cloud.multishop.controller.admin;

import cn.hutool.core.util.BooleanUtil;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.constant.RenovationType;
import com.jf.cloud.multishop.dto.ShopRenovationDTO;
import com.jf.cloud.multishop.model.ShopRenovation;
import com.jf.cloud.multishop.service.ShopRenovationService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 菠萝凤梨
 */
@RestController("adminShopRenovationUpDelController")
@RequestMapping("/mp/shop_renovation")
//@ConditionalOnProperty(prefix = "jfcloud_", name = "expose.operation.auth", havingValue = "true", matchIfMissing = true)
@Tag(name = "店铺页面-修改、删除、设为主页")
public class ShopRenovationUpDelController {

    @Autowired
    private ShopRenovationService shopRenovationService;

    @Autowired
    private MapperFacade mapperFacade;

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;


    @PutMapping("/update_pc")
    @Operation(summary = "更新店铺pc端装修信息" , description = "更新店铺装修信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ShopRenovationDTO shopRenovationDTO) {
        shopRenovationDTO.setRenovationType(RenovationType.PC.value());
        updateShopRenovation(shopRenovationDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping("/update_h5")
    @Operation(summary = "更新店铺移动端装修信息" , description = "更新店铺装修信息")
    public ServerResponseEntity<Void> updateH5(@Valid @RequestBody ShopRenovationDTO shopRenovationDTO) {
        shopRenovationDTO.setRenovationType(RenovationType.H5.value());
        updateShopRenovation(shopRenovationDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/delete_pc")
    @Operation(summary = "删除pc店铺装修信息" , description = "根据店铺装修信息id删除店铺pc装修信息")
    public ServerResponseEntity<Void> delete(@RequestParam Long renovationId) {
        deleteRenovation(renovationId);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/delete_h5")
    @Operation(summary = "删除h5店铺装修信息" , description = "根据店铺装修信息id删除店铺h5装修信息")
    public ServerResponseEntity<Void> deleteH5(@RequestParam Long renovationId) {
        deleteRenovation(renovationId);
        return ServerResponseEntity.success();
    }

    @PutMapping("/update_home_status_pc")
    @Operation(summary = "PC端修改主页标识" , description = "PC端修改主页标识")
    public ServerResponseEntity<Void> updateHomeStatus(@RequestParam Long renovationId) {
        updateShopRenovationStatus(renovationId);
        return ServerResponseEntity.success();
    }

    @PutMapping("/update_h5_home_status")
    @Operation(summary = "移动端修改主页标识" , description = "移动端修改主页标识")
    public ServerResponseEntity<Void> updateH5HomeStatus(@RequestParam Long renovationId) {
        updateShopRenovationStatus(renovationId);
        return ServerResponseEntity.success();
    }

    private void updateShopRenovationStatus(Long renovationId) {
        if (BooleanUtil.isFalse(permission)){
            throw new LuckException("抱歉，当前无操作权限");
        }
        ShopRenovation shopRenovation = shopRenovationService.getByRenovationId(renovationId);
        if (!Objects.equals(shopRenovation.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopRenovationService.updateToHomePage(shopRenovation);
        shopRenovationService.removeCache(shopRenovation.getShopId(), shopRenovation.getRenovationType(), shopRenovation.getRenovationId());
    }

    private void updateShopRenovation(ShopRenovationDTO shopRenovationDTO) {
        if (BooleanUtil.isFalse(permission)){
            throw new LuckException("抱歉，当前无操作权限");
        }
        ShopRenovation shopRenovation = mapperFacade.map(shopRenovationDTO, ShopRenovation.class);
        ShopRenovation shopRenovationDb = shopRenovationService.getByRenovationId(shopRenovation.getRenovationId());
        if (Objects.isNull(shopRenovationDb)) {
            throw new LuckException("该装修不存在，请刷新页面重试");
        }
        if (!Objects.equals(shopRenovationDb.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopRenovationService.update(shopRenovation);
        shopRenovationService.removeCache(shopRenovationDb.getShopId(), shopRenovationDTO.getRenovationType(), shopRenovationDb.getRenovationId());
    }

    private void deleteRenovation(Long renovationId) {
        if (BooleanUtil.isFalse(permission)){
            throw new LuckException("抱歉，当前无操作权限");
        }
        ShopRenovation shopRenovation = shopRenovationService.getByRenovationId(renovationId);
        if(!Objects.equals(shopRenovation.getShopId(), AuthUserContext.get().getTenantId())){
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopRenovationService.deleteById(renovationId);
        shopRenovationService.removeCache(shopRenovation.getShopId(), shopRenovation.getRenovationType(), shopRenovation.getRenovationId());
    }
}
